package cc.orange.mainView;

import android.content.Intent;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;

import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.R;
import cc.orange.adapter.HomeAdapter;
import cc.orange.base.BaseFragment;
import cc.orange.databinding.FragmentHomeBinding;
import cc.orange.entity.HightEntity;
import cc.orange.entity.IsLoginsEntity;
import cc.orange.entity.MsgInfosVideoEntity;
import cc.orange.entity.NoticeEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.popup.PopupHomeSpeedView;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import cc.orange.utils.ZXToastUtil;

public class HomeFragment extends BaseFragment implements View.OnClickListener{

    private HomeAdapter adapter;
    private ArrayList<MsgInfosVideoEntity.Data> list = new ArrayList<>();
    private FragmentHomeBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_home,container,false);
        initViews();
        returnVideo("3");
//        notice(); UI5不需要
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void initViews() {

//        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        binding.homeHfRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new HomeAdapter(list,1);
        binding.homeHfRecycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent intent = new Intent(getActivity(), VideoActivity.class);
//                intent.putExtra("uid",""+list.get(position).getSocVideo().getWomanId());
//                intent.putExtra("id",""+list.get(position).getSocVideo().getId());
//                intent.putExtra("video",list.get(position).getSocVideo().getVideo());
//                intent.putExtra("img",list.get(position).getSocVideo().getImage());
//                intent.putExtra("title",list.get(position).getSocVideo().getTitle());
//                intent.putExtra("name",list.get(position).getWomanName());
//                intent.putExtra("flag",list.get(position).getDianzan());
//                intent.putExtra("zNum",list.get(position).getDzNum());
//                startActivity(intent);
                if(list.get(position).getAge() == -1){
                    return;
                }
                ZXSharedPrefUtil sh  = new ZXSharedPrefUtil();
                boolean isLogin_talk = sh.getBool("isLogin_talk", false);
                if(isLogin_talk) {
                    Intent intent2 = new Intent(getActivity(), ConversationListActivity.class);
                    intent2.putExtra("uid", ""+list.get(position).getSocVideo().getWomanId());
                    intent2.putExtra("name", list.get(position).getWomanName());
                    startActivity(intent2);
                }else{
                    ZXToastUtil.showToast("请先登录");
                    startActivity(new Intent(getActivity(),LonginWxActivity.class));
                }
            }
        });

        binding.homeFragText1.setOnClickListener(this);
        binding.homeFragText2.setOnClickListener(this);
//        binding.homeFragText3.setOnClickListener(this);
        binding.homeFragText4.setOnClickListener(this);
        binding.homeFrgImg2.setOnClickListener(this);
    }



    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getHight(HightEntity entity) {//改变布局
        if(entity.getHight() > 0){
            RelativeLayout.LayoutParams pp = new RelativeLayout.
                    LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    entity.getHight());
            binding.homeTitle.setLayoutParams(pp);
        }
    }


    private void returnVideo(String classId){
        ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
        String id_talk = ss.getString("id_talk");
        RetrofitFactory.create(ApiService.class)
                .homeVideo(getTokens(),classId,id_talk)
                .enqueue(new DefaultCallback<MsgInfosVideoEntity>() {
                    @Override
                    public void onStart(Call<MsgInfosVideoEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<MsgInfosVideoEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<MsgInfosVideoEntity> call, MsgInfosVideoEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.addAll(baseEntity.getData());
                            if(list.size() == 0){
                                ZXToastUtil.showToast("暂无数据");
                            }else{
//                                if(list.size() > 4){
//                                    MsgInfosVideoEntity.Data dd = new MsgInfosVideoEntity.Data();
//                                    dd.setAge(-1);
//                                    list.add(3,dd);
//                                }else{
//                                    MsgInfosVideoEntity.Data dd = new MsgInfosVideoEntity.Data();
//                                    dd.setAge(-1);
//                                    list.add(dd);
//                                }
                            }
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    private void getSpeedVideo(String num) {
        ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
        String userId = ss.getString("id_talk", "");
        RetrofitFactory.create(ApiService.class)
                .getSpeedVideo(getTokens(),num, userId)
                .enqueue(new DefaultCallback<MsgInfosVideoEntity>() {
                    @Override
                    public void onStart(Call<MsgInfosVideoEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<MsgInfosVideoEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<MsgInfosVideoEntity> call, MsgInfosVideoEntity msgInfosVideoEntity) {
                        cancelLoading();
                        if(msgInfosVideoEntity.getCode() == 0){
                            list.addAll(msgInfosVideoEntity.getData());
                            if(list.size() == 0){
                                ZXToastUtil.showToast("暂无数据");
                            }else{
                                if(list.size() > 4){
                                    MsgInfosVideoEntity.Data dd = new MsgInfosVideoEntity.Data();
                                    dd.setAge(-1);
                                    list.add(4,dd);
                                }else{
                                    MsgInfosVideoEntity.Data dd = new MsgInfosVideoEntity.Data();
                                    dd.setAge(-1);
                                    list.add(dd);
                                }
                            }
                        }else{
                            ZXToastUtil.showToast(msgInfosVideoEntity.getMsg());
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    private void notice(){

        RetrofitFactory.create(ApiService.class)
                .notice()
                .enqueue(new DefaultCallback<NoticeEntity>() {
                    @Override
                    public void onStart(Call<NoticeEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<NoticeEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<NoticeEntity> call, NoticeEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            List<String> list_str = new ArrayList<>();
                            for (int i = 0; i < baseEntity.getData().size(); i++) {
                                list_str.add(baseEntity.getData().get(i).getNotice());
                            }

                            binding.homeHfText12.setInAnimation(R.anim.animation_down_up_in_animation)
                                    .setOutAnimation(R.anim.animation_down_up_out_animation)
                                    .bindData(list_str.toArray(new String[0]))
                                    .startSwitch(5000);
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHight(IsLoginsEntity entity) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.home_frag_text1:
                list.clear();
                returnVideo("3");
                initTab(1);
                break;
            case R.id.home_frag_text2:
                list.clear();
                returnVideo("1");
                initTab(2);
                break;
            case R.id.home_frag_text3:
                list.clear();
                returnVideo("2");
                initTab(3);
                break;
            case R.id.home_frg_img2:
                startActivity(new Intent(getActivity(), RankingActivity.class));
                break;
            case R.id.home_frag_text4:
                PopupHomeSpeedView.showPopupwindow(getActivity(), binding.homeScroll, new PopupHomeSpeedView.PopuStatus() {
                    @Override
                    public void popupShow() {

                    }

                    @Override
                    public void popupDismissClick2(String se) {
                        list.clear();
                        getSpeedVideo(se);
                        PopupHomeSpeedView.popDismiss();
                    }

                    @Override
                    public void popupDismissCancle(String se) {

                    }
                });
                break;
        }
    }

    private void initTab(int type){
        binding.homeHfText1.setTextSize(TypedValue.COMPLEX_UNIT_SP,type == 1 ? 22 : 17);
        binding.homeHfText2.setTextSize(TypedValue.COMPLEX_UNIT_SP,type == 2 ? 22 : 17);
        binding.homeHfText3.setTextSize(TypedValue.COMPLEX_UNIT_SP,type == 3 ? 22 : 17);

        binding.homeUi2Tab1.setVisibility(type == 1 ? View.VISIBLE : View.GONE);
        binding.homeUi2Tab2.setVisibility(type == 2 ? View.VISIBLE : View.GONE);
        binding.homeUi2Tab3.setVisibility(type == 3 ? View.VISIBLE : View.GONE);
    }


    @Override
    public void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }
}